package com.xuxin.nantai.model;


import com.xuxin.nantai.base.BaseConstant;
import com.xuxin.nantai.base.BaseObserver;
import com.xuxin.nantai.bean.AllAreaBean;
import com.xuxin.nantai.bean.HangUpdateLoginBean;
import com.xuxin.nantai.contract.AllAreaContract;
import com.xuxin.nantai.network.RetrofitApi;
import com.xuxin.nantai.network.Retrofits;
import com.xuxin.nantai.presenter.AllAreaPresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * Created by xu_xin
 * Created on 2020.06.22 0022
 */
public class AllAreaModel implements AllAreaContract.Model {
    @Override
    public void getDefaultArea(AllAreaPresenter presenter, String loginName) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getLoginUser(loginName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<HangUpdateLoginBean>() {
                    @Override
                    public void onNext(HangUpdateLoginBean bean) {
                        if (BaseConstant.success_code.equals(bean.getCode())) {
                            presenter.onSuccess(bean);
                        } else {
                            presenter.onError(bean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }

    @Override
    public void getAllArea(AllAreaPresenter presenter) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getAllArea()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<AllAreaBean>() {
                    @Override
                    public void onNext(AllAreaBean bean) {
                        if (BaseConstant.success_code.equals(bean.getCode())) {
                            presenter.onSuccess(bean);
                        } else {
                            presenter.onError(bean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }
}